
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "materialWidgetLight.h"

#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QGroupBox>

//#include "Movable.h"
//#include "ILight.h"
#include "DirectionalAt.h"
#include "SpotAt.h"
#include "Material.h"

//constructor
materialWidgetLight::materialWidgetLight(QWidget* parent) : baseMaterialWidget(parent)
{
	//ambient color
	QLabel* lbAmbientR = new QLabel("R=");
	QLabel* lbAmbientG = new QLabel("G=");
	QLabel* lbAmbientB = new QLabel("B=");
	_leAmbientR = new QLineEdit();
	_leAmbientG = new QLineEdit();
	_leAmbientB = new QLineEdit();
	QGridLayout* ambientLayout = new QGridLayout();
	ambientLayout->addWidget(lbAmbientR, 0, 0, 1, 1);
	ambientLayout->addWidget(_leAmbientR, 0, 1, 1, 1);
	ambientLayout->addWidget(lbAmbientG, 1, 0, 1, 1);
	ambientLayout->addWidget(_leAmbientG, 1, 1, 1, 1);
	ambientLayout->addWidget(lbAmbientB, 2, 0, 1, 1);
	ambientLayout->addWidget(_leAmbientB, 2, 1, 1, 1);
	QGroupBox* groupAmbient = new QGroupBox("Ambient");
	groupAmbient->setLayout(ambientLayout);


	//diffuse color
	QLabel* lbDiffuseR = new QLabel("R=");
	QLabel* lbDiffuseG = new QLabel("G=");
	QLabel* lbDiffuseB = new QLabel("B=");
	_leDiffuseR = new QLineEdit();
	_leDiffuseG = new QLineEdit();
	_leDiffuseB = new QLineEdit();
	QGridLayout* diffuseLayout = new QGridLayout();
	diffuseLayout->addWidget(lbDiffuseR, 0, 0, 1, 1);
	diffuseLayout->addWidget(_leDiffuseR, 0, 1, 1, 1);
	diffuseLayout->addWidget(lbDiffuseG, 1, 0, 1, 1);
	diffuseLayout->addWidget(_leDiffuseG, 1, 1, 1, 1);
	diffuseLayout->addWidget(lbDiffuseB, 2, 0, 1, 1);
	diffuseLayout->addWidget(_leDiffuseB, 2, 1, 1, 1);
	QGroupBox* groupDiffuse = new QGroupBox("Diffuse");
	groupDiffuse->setLayout(diffuseLayout);

	//specular color
	QLabel* lbSpecularR = new QLabel("R=");
	QLabel* lbSpecularG = new QLabel("G=");
	QLabel* lbSpecularB = new QLabel("B=");
	_leSpecularR = new QLineEdit();
	_leSpecularG = new QLineEdit();
	_leSpecularB = new QLineEdit();
	QGridLayout* specularLayout = new QGridLayout();
	specularLayout->addWidget(lbSpecularR, 0, 0, 1, 1);
	specularLayout->addWidget(_leSpecularR, 0, 1, 1, 1);
	specularLayout->addWidget(lbSpecularG, 1, 0, 1, 1);
	specularLayout->addWidget(_leSpecularG, 1, 1, 1, 1);
	specularLayout->addWidget(lbSpecularB, 2, 0, 1, 1);
	specularLayout->addWidget(_leSpecularB, 2, 1, 1, 1);
	QGroupBox* groupSpecular = new QGroupBox("Specular");
	groupSpecular->setLayout(specularLayout);

	//widget layout
	QGridLayout* layout = new QGridLayout();
	layout->addWidget(groupAmbient, 0, 0, 1, 2);
	layout->addWidget(groupDiffuse, 1, 0, 1, 2);
	layout->addWidget(groupSpecular, 2, 0, 1, 2);
	layout->addWidget(new QWidget(), 3, 0, 1, 2);
	setLayout(layout);

	//connection of the ambient
	connect(_leAmbientR, SIGNAL(textEdited(const QString&)), this, SLOT(setLightAmbient()));
	connect(_leAmbientG, SIGNAL(textEdited(const QString&)), this, SLOT(setLightAmbient()));
	connect(_leAmbientB, SIGNAL(textEdited(const QString&)), this, SLOT(setLightAmbient()));

	//connection of the diffuse
	connect(_leDiffuseR, SIGNAL(textEdited(const QString&)), this, SLOT(setLightDiffuse()));
	connect(_leDiffuseG, SIGNAL(textEdited(const QString&)), this, SLOT(setLightDiffuse()));
	connect(_leDiffuseB, SIGNAL(textEdited(const QString&)), this, SLOT(setLightDiffuse()));

	//connection of the specular
	connect(_leSpecularR, SIGNAL(textEdited(const QString&)), this, SLOT(setLightSpecular()));
	connect(_leSpecularG, SIGNAL(textEdited(const QString&)), this, SLOT(setLightSpecular()));
	connect(_leSpecularB, SIGNAL(textEdited(const QString&)), this, SLOT(setLightSpecular()));

}

//destructor
materialWidgetLight::~materialWidgetLight(){}

//fill the widget with values
void materialWidgetLight::fillWidget(Movable* m)
{
	//no light
	if( m == 0)
		return;

	//movable is not a light
	if(m->getMovableType() != MT_LIGHT)
		return;

	//get the light
	GUIILight* light = (GUIILight*)m;

	if(light->getType() == LT_DIRECTIONAL_AT)
	{
		DirectionalAt* lightAt = (DirectionalAt*)light;
		_currentLight = (GUIILight*)lightAt->getDirectionalLightElement();
	}
	else if (light->getType() == LT_SPOT_AT)
	{
		SpotAt* lightAt = (SpotAt*)light;
		_currentLight = (GUIILight*)lightAt->getSpotLightElement();
	}
	else
		_currentLight = light;

	//fill the ambient
	float* ambient = _currentLight->getMaterial().getAmbient();
	_leAmbientR->setText(QString::number(ambient[0]));
	_leAmbientG->setText(QString::number(ambient[1]));
	_leAmbientB->setText(QString::number(ambient[2]));

	//fill the diffuse
	float* diffuse = _currentLight->getMaterial().getDiffuse();
	_leDiffuseR->setText(QString::number(diffuse[0]));
	_leDiffuseG->setText(QString::number(diffuse[1]));
	_leDiffuseB->setText(QString::number(diffuse[2]));

	//fill the specular
	float* specular = _currentLight->getMaterial().getSpecular();
	_leSpecularR->setText(QString::number(specular[0]));
	_leSpecularG->setText(QString::number(specular[1]));
	_leSpecularB->setText(QString::number(specular[2]));
}

//set the light ambient color
void materialWidgetLight::setLightAmbient()
{
	_currentLight->getMaterial().setAmbient(colorQStringToFloat(_leAmbientR->text()),
		colorQStringToFloat(_leAmbientG->text()),
		colorQStringToFloat(_leAmbientB->text()),
		1);
}

//set the light diffuse color
void materialWidgetLight::setLightDiffuse()
{
	_currentLight->getMaterial().setDiffuse(colorQStringToFloat(_leDiffuseR->text()),
		colorQStringToFloat(_leDiffuseG->text()),
		colorQStringToFloat(_leDiffuseB->text()),
		1);
}

//set the light specular color
void materialWidgetLight::setLightSpecular()
{
	_currentLight->getMaterial().setSpecular(colorQStringToFloat(_leSpecularR->text()),
		colorQStringToFloat(_leSpecularG->text()),
		colorQStringToFloat(_leSpecularB->text()),
		1);
}
